﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VehicleScale.DataAccess;
using VehicleScale.Models;
using System.IO.Ports;
using System.Drawing;
using System.IO;
using System.Text.RegularExpressions;
using System.Drawing.Printing;

namespace VehicleScale
{
    public class Common
    {
        #region Layout Fields
        public Size SizeUpdate = new Size(600, 400);
        public Size SizeList = new Size(800, 600);
        #endregion

        #region Fields

        public static EmployeeModel UserLogin;
        public static CompanyInfoModel CompanyInfo;
        public static ConfigModel Config;
        public static SerialPort SerialPort;

        public static string outputPath = "C:\\output.out";
        public static string RememberPath = Path.GetTempPath() + "remeberfile.vss";
        public static string MyAcc = "nimda";
        public static string MyPass = "090";
        public static string Server;
        public static string Database;
        public static string UserRestore = "huutai";
        public static string PasswordRestore = "huutai";
        public static string PasswordDefault = "1234";

        public static string PassToScale = "4321";

        #endregion

        #region List Fields
        public static IList<CustomerModel> Customers;
        public static IList<CategoryModel> Categories;
        public static IList<ProductModel> Products;
        public static IList<string> SelectedCustomerIDs = new List<string>();
        #endregion

        #region Load methods
        public static void LoadCustomerObject()
        {
            ICustomerDataAccess dataAccess = new CustomerDataAccess();
            Customers = dataAccess.GetAll();
        }

        public static void LoadCategoryObject()
        {
            ICategoryDataAccess dataAccess = new CategoryDataAccess();
            Categories = dataAccess.GetAll();
        }

        public static void LoadProductObject()
        {
            IProductDataAccess dataAccess = new ProductDataAccess();
            Products = dataAccess.GetAll();
        }

        public static void LoadSerialPortObject()
        {
            try
            {
                ParaPort paraPort = new ParaPort();
                if (Common.SerialPort == null)
                    Common.SerialPort = new System.IO.Ports.SerialPort();
                
                if (!Common.SerialPort.IsOpen)
                {
                    // Config ports.
                    Common.SerialPort.PortName = paraPort.PortName;
                    Common.SerialPort.BaudRate = paraPort.BaudRate;
                    Common.SerialPort.Parity = (System.IO.Ports.Parity)paraPort.Parity;
                    Common.SerialPort.DataBits = paraPort.DataBits;
                    Common.SerialPort.StopBits = (System.IO.Ports.StopBits)paraPort.StopBits;
                }
            }
            catch (Exception ex)
            {
                Library.Tools.MsgBox.Show("Lỗi KẾT NỐI thiết bị cân! Error details: " + ex.Message);
            }
        }
        #endregion

        #region Convert methods
        public static byte[] ConvertToByte(Image imageOriginal)
        {
            if (imageOriginal == null)
                return null;
            else if (imageOriginal == global::VehicleScale.Properties.Resources.No_Photo)
                return null;

            Bitmap bitmap = new Bitmap(imageOriginal);
            MemoryStream stream = new MemoryStream();
            bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
            return stream.GetBuffer();
        }

        public static Image ConvertToImage(byte[] byteOriginal)
        {
            //bool isSaveImage = false;
            //Image image = ScaleToSize(global::VehicleScale.Properties.Resources.no_photo, ref isSaveImage);
            //if (isSaveImage)
            //    image.Save("D:\\No_Photo.png", System.Drawing.Imaging.ImageFormat.Png);
            if (byteOriginal == null) return global::VehicleScale.Properties.Resources.No_Photo;
            return System.Drawing.Bitmap.FromStream(new System.IO.MemoryStream(byteOriginal));
        }

        public static Image ScaleToSize(Bitmap pBitmap, ref bool bSaveImg)
        {
            bSaveImg = false;
            float width = pBitmap.PhysicalDimension.Width;
            float height = pBitmap.PhysicalDimension.Height;
            if ((width < 61.0) && (height < 61.0))
            {
                return (Image)pBitmap.Clone();
            }
            if (width == height)
            {
                bSaveImg = true;
                return pBitmap.GetThumbnailImage(60, 60, null, IntPtr.Zero);
            }
            if (width > height)
            {
                bSaveImg = true;
                double num3 = 60f / width;
                double num4 = (height * num3) + 0.5;
                return pBitmap.GetThumbnailImage(60, (int)num4, null, IntPtr.Zero);
            }
            if (width < height)
            {
                bSaveImg = true;
                double num5 = 60f / height;
                double num6 = (width * num5) + 0.5;
                return pBitmap.GetThumbnailImage((int)num6, 60, null, IntPtr.Zero);
            }
            bSaveImg = true;
            return pBitmap.GetThumbnailImage(60, 60, null, IntPtr.Zero);
        }
        #endregion

        public static bool RegexMatch(string input, string prefix, int length)
        {
            string pattern = "^(" + prefix + ")\\d{0," + length + "}$";
            return new Regex(pattern).IsMatch(input);
        }

        public static bool CheckPrinter(string printerName)
        {
            foreach (var item in PrinterSettings.InstalledPrinters)
            {
                if (item == printerName)
                    return true;
            }
            return false;
        }

        
    }
}
